﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace DFGK.Service.BBS
{
    public class ShareFileService : BaseService, IShareFileService
    {
        private readonly IShareFileRepository _shareFileRepo;
        private readonly IUnitOfWork _unitOfWork;
        public ShareFileService(IMapper mapper, IShareFileRepository shareFileRepo, IUnitOfWork unitOfWork) : base(mapper)
        {
            _shareFileRepo = shareFileRepo;
            _unitOfWork = unitOfWork;
        }

        public List<ShareFileInfo> GetShareFileInfoPageData(string fileTile, out int totalCount, int pageIndex = 1, int pageSize = 20)
        {
            var query = _shareFileRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(fileTile))
            {
                query = query.Where(t => t.Tile == fileTile);
            }
            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }
        public bool SubmitShareFileData(ShareFileInfo model, out string errorMsg)
        {
            errorMsg = "";
            if (model.Id > 0)
            {
                _shareFileRepo.UpdEntity(model);
            }
            else
            {
                _shareFileRepo.AddEntity(model);
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public ShareFileInfo? GetShareFileInfo(long shareInfoId)
        {
            return _shareFileRepo.QueryEntityById(shareInfoId);
        }

        public bool DelShareInfo(long shareInfoId)
        {
            var shareFileInfo = _shareFileRepo.QueryEntityById(shareInfoId);
            if (shareFileInfo == null)
                return false;
            _shareFileRepo.DelEntity(shareFileInfo);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
